package imagenes;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class JPanelImage extends JPanel{
	
	protected int w;
	protected int h;
	protected int wResized;
	protected int hResized;
	protected float scale;
	
	public JPanelImage(int w1, int h1){  
		w = w1;
		h = h1;
		this.setSize(w,h);
	}    
	 
	public void paint(Graphics g, ImageIcon imagenFondo){
		g.clearRect(0, 0, w, h);//borra la imagen anterior 
		g.drawImage(imagenFondo.getImage(),0,0,wResized,hResized, null);                
		setOpaque(false);
		super.paintComponent(g);	
	} 
	
	//metodo par conservar las proporciones de la imagen
	private void reScale(){
		if(w >= h){
			wResized = w;
			hResized = (int) (w / scale);
		}else{
			hResized = h;
			wResized = (int) (h / scale);
		}
	}
	    
	public void setFoto(String dir_foto){
		if(dir_foto != null){
			ImageIcon imagenFondo = new ImageIcon(dir_foto); 
			float auxh = imagenFondo.getIconHeight();
			float auxw = imagenFondo.getIconWidth();
			if(auxh >= auxw)
				scale = auxh / auxw;
			else
				scale = auxw / auxh;
			reScale();
		
			paint(getGraphics(), imagenFondo);
		}
	}
	
}
